from PySide import QtGui
import g_editnodes
import g_editmaterials
import b_standard1D
import b_tension1D
import b_compression1D

class EditMembers(QtGui.QWidget):
	
	def __init__(self, parent):
		super(EditMembers, self).__init__()
		self.parent = parent
		self.errormsg = QtGui.QErrorMessage()
		self.initUI()

	def initUI(self):
		self.listmembers = QtGui.QListWidget()
		self.listmembers.addItems(self.parent.data.members.keys())
		self.listmembers.sortItems()
		self.listmembers.itemSelectionChanged.connect(self.selectionChanged)
		
		row = 0
		grid = QtGui.QGridLayout()
		name = QtGui.QLabel('Name')
		self.nameedit = QtGui.QLineEdit()
		grid.addWidget(name, row, 0)
		grid.addWidget(self.nameedit, row, 1)
		row += 1
		position = QtGui.QLabel('Nodes')
		grid.addWidget(position, row, 0)
		self.nodeedit = []
		for i in range(0, 2):
			self.nodeedit.append(QtGui.QComboBox())
			self.nodeedit[i].addItems(self.parent.data.nodes.keys())
			grid.addWidget(self.nodeedit[i], row, 1)
			row += 1
		material = QtGui.QLabel('Material')
		self.materialedit = QtGui.QComboBox()
		self.materialedit.addItems(self.parent.data.materials.keys())
		grid.addWidget(material, row, 0)
		grid.addWidget(self.materialedit, row, 1)
		row += 1
		
		self.addButton = QtGui.QPushButton("Add Member")
		self.addButton.clicked.connect(self.addMember)
		self.modifyButton = QtGui.QPushButton("Modify Member")
		self.modifyButton.clicked.connect(self.modifyMember)
		self.deleteButton = QtGui.QPushButton("Delete Member")
		self.deleteButton.clicked.connect(self.deleteMember)
		buttons = QtGui.QHBoxLayout()
		buttons.addWidget(self.addButton)
		buttons.addWidget(self.modifyButton)
		buttons.addWidget(self.deleteButton)
		
		editdialog = QtGui.QVBoxLayout()
		editdialog.addLayout(grid)
		editdialog.addStretch(1)
		editdialog.addLayout(buttons)
		
		hbox = QtGui.QHBoxLayout()
		hbox.addWidget(self.listmembers)
		hbox.addLayout(editdialog)
		
		self.setLayout(hbox)
		self.setWindowTitle("Edit members")
	
	def modifyMember(self):
		current = self.listmembers.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			nameedit = str(self.nameedit.text())
			if nameedit != name:
				if nameedit in self.parent.data.members.keys():
					self.errormsg.showMessage("Member by that name already exists")
					return
				self.parent.data.members[name].name = nameedit
				self.parent.data.members[nameedit] = self.parent.data.members[name]
				del self.parent.data.members[name]
			self.parent.updateMember(self.parent.data.members[nameedit])
			nodes = []
			for i in range(0, 2):
				node = str(self.nodeedit[i].currentText())
				if not node in self.parent.data.nodes.keys():
					self.errormsg.showMessage("Node " + node + " does not exist")
					return
				nodedata = self.parent.data.nodes[node]
				if nodedata in nodes:
					self.errormsg.showMessage("Multiple references to " + node + ": Nodes must be distinct")
					return
				nodes.append(self.parent.data.nodes[node])
			mat = str(self.materialedit.currentText())
			if not mat in self.parent.data.materials.keys():
				self.errormsg.showMessage("Material by that name doesn't exist")
				return
			material = self.parent.data.materials[mat]
			self.parent.data.members[nameedit].nodes = nodes
			self.parent.data.members[nameedit].material = material
			if name == nameedit:
				self.parent.updateAll(self)
			else:
				self.parent.updateAll()
		else:
			self.errormsg.showMessage("No member selected to modify")
	
	def addMember(self):
		name = str(self.nameedit.text())
		if name in self.parent.data.members.keys():
			self.errormsg.showMessage("Member by that name already exists, cannot add")
			return
		nodes = []
		for i in range(0, 2):
			node = str(self.nodeedit[i].currentText())
			if not node in self.parent.data.nodes.keys():
				self.errormsg.showMessage("Node " + node + " does not exist")
				return
			nodedata = self.parent.data.nodes[node]
			if nodedata in nodes:
				self.errormsg.showMessage("Multiple references to " + node + ": Nodes must be distinct")
				return
			nodes.append(self.parent.data.nodes[node])
		mat = str(self.materialedit.currentText())
		if not mat in self.parent.data.materials.keys():
			self.errormsg.showMessage("Material by that name doesn't exist")
			return
		material = self.parent.data.materials[mat]
		member = b_standard1D.Standard1D(self.parent.data, name, nodes, material)
		self.parent.data.members[name] = member
		self.parent.layout.view.addMember(member)
		self.parent.updateAll(self)
	
	def deleteMember(self):
		current = self.listmembers.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			member = self.parent.data.members[name]
			member.deleteMember()
			self.parent.layout.view.deleteMember(member)
			self.parent.updateAll(self)
		else:
			self.errormsg.showMessage("No member selected to delete")
	
	def updateMembers(self, check=None):
		if isinstance(check, g_editmaterials.EditMaterials):
			#print "Update Members Materials"
			self.materialedit.clear()
			self.materialedit.addItems(self.parent.data.materials.keys())
			self.updateList()
		if isinstance(check, g_editnodes.EditNodes):
			#print "Update Members Nodes"
			for i in range(0, 2):
				self.nodeedit[i].clear()
				self.nodeedit[i].addItems(self.parent.data.nodes.keys())
			self.updateList()
		if check is self:
			self.updateList()
	
	def updateList(self):
		#print "Update Members List"
		self.listmembers.itemSelectionChanged.disconnect(self.selectionChanged)
		self.listmembers.clear()
		self.listmembers.addItems(self.parent.data.members.keys())
		self.listmembers.sortItems()
		self.listmembers.itemSelectionChanged.connect(self.selectionChanged)
	
	def selectionChanged(self):
		current = self.listmembers.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			self.nameedit.setText(name)
			for i in range(0, 2):
				self.nodeedit[i].setCurrentIndex(self.parent.data.nodes.keys().index(self.parent.data.members[name].nodes[i].name))
			self.materialedit.setCurrentIndex(self.parent.data.materials.keys().index(self.parent.data.members[name].material.name))
	
	def closeEvent(self, event):
		self.hide()
